package cn.sinata.wanglin.village.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.SameCityListAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.SameCityInfo;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
/**
 *小区---同城八卦的list.
 *
 */
public class SameCityListFragment extends RecyclerViewFragment {

    private BaseRecyclerAdapter mAdapter;
    private List<SameCityInfo> mSameCityInfos = new ArrayList<>();
    private String classifyId;
    private int pageNo = 0;
    public static SameCityListFragment newInstance(String classifyId){
        Utils.systemErr("--classifyId-->"+classifyId);
        SameCityListFragment mSameCityListFragment = new SameCityListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("classifyId",classifyId);
        mSameCityListFragment.setArguments(bundle);
        return mSameCityListFragment;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if(mAdapter==null){
            HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .size(DensityUtil.dip2px(context,10))
                    .color(ContextCompat.getColor(context, R.color.dividing_line_color_wanglin))
                    .build();
            setItemDecoration(itemDecoration);
            mAdapter = new SameCityListAdapter(mSameCityInfos,getActivity(),SameCityListFragment.this);
        }
        return mAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {
        setEmptyViewClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
                pullDownRefresh();
            }
        });
        if (getArguments()!=null){
            classifyId =  getArguments().getString("classifyId");
        }
        getData(classifyId);
    }
    @Override
    protected void onVisibleToUser() {

    }
    @Override
    protected void onInvisibleToUser() {

    }
    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData(classifyId);
    }
public void refresh(){
    showDialog("刷新中...");
    pullDownRefresh();
}
    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData(classifyId);
    }
    private void getData(String classifyId) {
        String userId= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getGossipComment(userId,classifyId,MyApplication.cityCode,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<SameCityInfo>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<SameCityInfo> data) {
                        if (pageNo == 0) {
                            mSameCityInfos.clear();
                        }
                        if (data.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mSameCityInfos.addAll(data);
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Utils.systemErr("=resultCod-->" + resultCode);
        Utils.systemErr("requestCode-->" + requestCode);
        if(resultCode==-1){
            switch (requestCode)
            {
                case 12:
                    showDialog("刷新中...");
                    pullDownRefresh();
                    break;
            }

        }
    }
}
